//
//  UserInfoViewController.swift
//  AKTest
//
//  Created by ZDST2018 on 2021/8/25.
//

import UIKit


enum Gender {
    case male
    case female
    case notSelectd
}


class UserInfoViewController: UIViewController {
    
    //    @IBOutlet weak var birthPicker: UIPickerView!
    @IBOutlet weak var birthPicker: UIDatePicker!
    @IBOutlet weak var maleBtn: UIButton!
    @IBOutlet weak var femaleBtn: UIButton!
    @IBOutlet weak var konwSwift: UISwitch!
    @IBOutlet weak var swiftLevelSlider: UISlider!
    @IBOutlet weak var changePassion: UIStepper!
    @IBOutlet weak var updateBtn: UIButton!
    
    @IBOutlet weak var imgH: NSLayoutConstraint!
    @IBOutlet weak var imgW: NSLayoutConstraint!
    
    lazy var bag = DisposeBag.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        birthPicker.layer.borderWidth = 1
        
        let birthObs: Observable<Bool> = birthPicker.rx.date.map({ (date: Date) -> Bool in
            let calen = NSCalendar.current
            let compare = calen.compare(date, to: Date.init(), toGranularity: .day)
            return compare == .orderedAscending ? true : false
        })
        birthObs.map({ (b: Bool) -> CGColor in
           return b == true ? UIColor.green.cgColor : UIColor.clear.cgColor
        }).subscribe {[weak self] eve in
            switch eve{
            case .next:
                if let cgC = eve.element {
                    self?.birthPicker.layer.borderColor = cgC
                }
            default :
                break
            }
        }.disposed(by: bag)
        
        let gender = BehaviorRelay<Gender>(value: Gender.notSelectd)
        
        Observable.combineLatest(birthObs, gender).map { (bool: Bool, gender: Gender) -> Bool in
            return bool && gender != .notSelectd
        }.bind(to: self.updateBtn.rx.isEnabled).disposed(by: bag)
        
        //等价写法
//        Observable.combineLatest(birthObs, gender).map { (bool: Bool, gender: Gender) -> Bool in
//            return bool && gender != .notSelectd
//
//        }.subscribe {[unowned self] eve in
//            switch eve{
//            case .next:
//                if let val = eve.element {
//                    self.updateBtn.isEnabled = val
//                }
//            default :
//                break
//            }
//
//        }.disposed(by: bag)
        
        
        //        maleBtn.rx.tap.map({ () -> Gender in
        //            return .male
        //        }).bind(to: gender).disposed(by: bag)
        //等价上面方法，accept（接收）
        maleBtn.rx.tap.map({ () -> Gender in
            return .male
        }).subscribe { eve in
            switch eve{
            case.next:
                if let seletc = eve.element {
                    gender.accept(seletc)
                }
            default:
                break
            }
        }.disposed(by: bag)
        
        
        femaleBtn.rx.tap.map({ () -> Gender in
            return .female
        }).bind(to: gender).disposed(by: bag)
        
        gender.subscribe {[weak self] eve in
            switch eve {
            //
            case .next:
                if let gender = eve.element {
                    switch gender {
                    case .female:
                        self?.femaleBtn.setImage(UIImage.init(named: "icon_selected_yes"), for: .normal)
                        self?.maleBtn.setImage(UIImage.init(named: "icon_selected_no"), for: .normal)
                    case .male:
                        self?.femaleBtn.setImage(UIImage.init(named: "icon_selected_no"), for: .normal)
                        self?.maleBtn.setImage(UIImage.init(named: "icon_selected_yes"), for: .normal)
                    default:
                        break
                    }
                }
                
            default:
                break
            }
        }.disposed(by: bag)
        
        
        let konwSwiftObs: ControlProperty<Bool> = konwSwift.rx.isOn
        
        konwSwiftObs.map { (b: Bool) -> Float in
            return b == true ? 0.25 : 0.0
        }.bind(to: self.swiftLevelSlider.rx.value).disposed(by: bag)
        // 等价写法
//        konwSwiftObs.map { (b: Bool) -> Float in
//            return b == true ? 0.25 : 0.0
//        }.subscribe { eve in
//            switch eve{
//            case .next:
//                if let vv = eve.element {
//                    self.swiftLevelSlider.value = vv
//                }
//            default:
//                break
//            }
//        }.disposed(by: bag)
        self.swiftLevelSlider.rx.value.map({(f : Float) -> Bool in
            return f == 0 ? false : true
        }).subscribe {[weak self] eve in
            switch eve{
            case .next:
                if let vv = eve.element {
                    self?.konwSwift.isOn = vv
                }
            default:
                break
            }
        }.disposed(by: bag)
        
        changePassion.setDecrementImage(UIImage.init(named: "icon_selected_yes"), for: .normal)
        changePassion.rx.value.subscribe { eve in
            switch eve{
            case .next:
                if let vv = eve.element {
                    self.imgH.constant =  self.imgH.constant + CGFloat(vv)
                    self.imgW.constant = self.imgW.constant + CGFloat(vv)
                }
            default:
                break
            }
        }.disposed(by: bag)

    }
    
    
    
    deinit {
        print("UserInfoViewController 释放了")
    }
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
